package com.i2works.smartluolong.web.service.flow.impl;

import com.i2works.smartluolong.utils.base.GlobalConstant;
import com.i2works.smartluolong.utils.basecommon.BaseCommonServiceImpl;
import com.i2works.smartluolong.utils.exception.DAOException;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.dal.flow.dao.IFlowTemplateDAO;
import com.i2works.smartluolong.web.entity.flow.FlowTemplateDO;
import com.i2works.smartluolong.web.entity.flow.FlowTemplateQuery;
import com.i2works.smartluolong.web.service.flow.IFlowTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 工作流模板业务层实现类
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/3/28 11:08
 */

@Service("flowTemplateService")
public class FlowTemplateServiceImpl extends BaseCommonServiceImpl<FlowTemplateQuery, FlowTemplateDO> implements IFlowTemplateService {

    @Autowired
    private IFlowTemplateDAO flowTemplateDAO;


    /**
     * 批量添加工作流模板
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void saveBatch(List<FlowTemplateDO> list, String userName) throws ServiceException {
        try {
            //根据flowId删除原始模板
            Integer flowId = list.get(0).getFlowId();
            flowTemplateDAO.deleteByFlowId(flowId);
            //重新添加
            for (FlowTemplateDO templateDO : list) {
                templateDO.setStatus(GlobalConstant.status.TRUE);
                templateDO.setUpdateName(userName);
                templateDO.setCreateName(userName);
                flowTemplateDAO.save(templateDO);
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    /**
     * 根据工作流ID删除流程模板
     */
	@Override
	public void deleteByFlowId(Integer flowId) throws ServiceException {
		try {
			flowTemplateDAO.deleteByFlowId(flowId);
		} catch (DAOException e) {
			 throw new ServiceException(e);
		}
		
	}

}
